<template>
    <div>
        <el-dialog :title="title" :visible.sync="addScoreVisible" append-to-body 
                   :before-close="handleCancel" :close-on-click-modal="false" @open="openAddScore()">
            <el-form label-position="right" label-width="80px" :model="scoreInfo" :rules="rules" ref="FormData"
                     class="demo-ruleForm">
                <el-form-item label="学生" prop="studentId">
                  <el-select v-model="FormData.StudentId" @change="changeSelect" placeholder="请选择学生">
                    <el-option
                            v-for="item in SList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            :disabled="item.disabled">
                    </el-option>
                  </el-select>
                </el-form-item>
                
                <el-form-item label="课程" prop="courseId">
                  <el-select v-model="FormData.CourseId" placeholder="请选择课程">
                    <el-option
                            v-for="item in CList"
                            :key="item.id"
                            :label="item.courseName"
                            :value="item.id"
                            :disabled="item.disabled">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="分数" prop="grade">
                  <el-input v-model="FormData.Grade"></el-input>
                </el-form-item>
                
                
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="onSubmit('FormData')">添加成绩</el-button>
                <el-button @click="resetForm('FormData')">重置信息</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import ajax from "@/lib/ajax";
    export default {
        name: "CreateScore",
        props: {
            addScoreVisible: Boolean,
            data: [Object, Boolean],
            title: String,
            show: {
            type: Boolean
            },           
            SList: {},
            CList: {},
            Edit: {
              type: Boolean
            },
            allList: {},

            FormData: {
              type: Object,
              default: () => {}
            }
        },
        data() {
            
            return {
                scoreInfo: {
                    scoreId: '',
                    scoreName: '',
                    realName: '',
                    identity: '',
                    sex: '1',
                    address: '',
                    phone: '',
                    position: '',
                    roleId: '1',
                    roleName: '',
                    scoreStatus: '1',
                    password: '',
                    checkPassword: ''
                },
                rules: {
                    
                    
                    
                }
            }
        },
        methods: {
            openAddScore() {
                this.$nextTick(function () {
                    this.$refs['FormData'].resetFields();
                    for (var key in this.entity) {
                        if (this.entity.hasOwnProperty(key)) {
                            console.log(this.entity[key] = '') ;
                        }
                    }
                });
            },
            //级联下拉框
            changeSelect() {
              console.log(this.FormData.StudentId)
              var that =this;
              // 清空内容
              this.FormData.CourseId = ''
              ajax
                .post("/api/services/app/Score/FindByStudentId", {
                  StudentId: that.FormData.StudentId, 
                  CourseId : 1                
                })
                .then(function (res) {
                  console.log(res.data.result, "]]]]]]]");
                  that.$emit('Coursevent',res.data.result);
                  
                })
                .catch(function () {
                  console.log("error1");
                });      
              
            },
            //关闭弹窗
            handleCancel() {
                this.$refs['FormData'].resetFields();
                this.$emit('closeevent');
            },
            onSubmit(formName) {
               var that =this;
                ajax
                  .post("/api/services/app/Score/CreateScore", {
                    StudentId: that.FormData.StudentId,
                    CourseId: that.FormData.CourseId,
                    Grade : that.FormData.Grade
                  })
                  .then((res) => {
                    // console.log(res,"新增")
                    this.$message.success("添加成功"); //新增成功消息提示             
                    this.$emit('closeevent');
                    this.$refs['FormData'].resetFields();
                    this.$emit('tabListevent');
                  })
                  .catch((res) => {
                    console.log(JSON.stringify(res))
                    this.$message.error("添加失败");
                  });
                
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            
        }
    }
</script>

<style scoped>

</style>